package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DoctorMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.Hosregister;
import com.kkb.pojo.HosregisterExample;
import com.kkb.utils.RandomUtil;
import com.kkb.vo.DoctorVo;
import com.kkb.vo.HosRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author:CoolTiger
 * @title: HosregisterService
 * @projectName ssm_hospital
 * @description: TODO
 * @date:2022/07/09/23:01:08
 **/
@Service
public class HosregisterService {
    @Autowired
    private HosregisterMapper hosregisterMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 多条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Hosregister> queryAll(Integer pageNum, Integer pageSize, HosRegisterVO vo) {
        //创建多条件查询容器
        HosregisterExample example = new HosregisterExample();
        HosregisterExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            //病历号
            if (vo.getHosRRecode() != null) {
                criteria.andHosrRecodeEqualTo(vo.getHosRRecode());
            }
            //主治医生
            if (vo.getDoctorName() != null || !"".equals(vo.getDoctorName().trim())) {
                int doctorId = 0;
                try {
                    doctorId = doctorMapper.findDoctorByName(vo.getDoctorName());
                } catch (Exception e) {

                }
                if (doctorId != 0) {
                    criteria.andDIdBetween(doctorId - 1, doctorId + 1);
                }
            }
            //科室
            if (vo.getDoctorKeshi() != null || !"".equals(vo.getDoctorKeshi().trim())) {
                criteria.andDKeshiLike("%" + vo.getDoctorKeshi() + "%");
            }
            //挂号时间
            if (vo.getBeginTime() != null) {
                criteria.andHosrTimeGreaterThanOrEqualTo(vo.getBeginTime());
            }
            if (vo.getEndTime() != null) {
                criteria.andHosrTimeLessThanOrEqualTo(vo.getEndTime());

            }

        }
        //分页条件
        PageHelper.startPage(pageNum, pageSize);
        List<Hosregister> hosregisters = hosregisterMapper.selectByExample(example);
        //通过医生编号查询名字
        for (Hosregister hosregister : hosregisters) {
            Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getdId());
            hosregister.setDoctorName(doctor.getdName());
        }
        return new PageInfo<>(hosregisters);
    }

    /**
     * 添加挂号表信息
     *
     * @param hosregister
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addHosRegister(Hosregister hosregister) {
        hosregister.setHosrTime(new Date());
        hosregister.setHosrRecode(RandomUtil.getCurrentTime());
        //通过医生姓名查询id
        int doctorId = doctorMapper.findIdByName(hosregister.getDoctorName());
        hosregister.setdId(doctorId);

        return hosregisterMapper.insertSelective(hosregister);
    }

    /**
     * 退号
     *
     * @param hosRegisterId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int eixtHosRegister(Integer hosRegisterId) {
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(hosRegisterId);
        hosregister.setHosrState(3);
        return hosregisterMapper.updateByPrimaryKeySelective(hosregister);
    }

    /**
     * 通过id去查询挂号表的所有信息
     *
     * @param hosRegisterId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Hosregister queryById(Integer hosRegisterId) {
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(hosRegisterId);
        //通过id医生名字查询
        Doctor doctor = doctorMapper.selectByPrimaryKey(hosregister.getdId());
        hosregister.setDoctorName(doctor.getdName());
        return hosregister;
    }

    /**
     * 更新挂号表
     *
     * @param hosregister
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateHosRegister(Hosregister hosregister) {
        ///通过医生姓名查询id
        int doctorId = doctorMapper.findIdByName(hosregister.getDoctorName());
        hosregister.setdId(doctorId);

        //通过id医生名字查询
        Hosregister hosregister1 = hosregisterMapper.selectByPrimaryKey(hosregister.getHosrId());
        hosregister.setHosrRecode(hosregister1.getHosrRecode());
        hosregister.setHosrTime(hosregister1.getHosrTime());
        return hosregisterMapper.updateByPrimaryKeySelective(hosregister);
    }

    /**
     * 查询科室信息
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Doctor> findKeshi(){
        return doctorMapper.findKeshi();
    }

    /**
     * 通过科室查询医生
     * @param keshi
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Doctor> findDoctorByKeshi(String keshi){
        return doctorMapper.findDoctor(keshi);
    }

}
